<?php
class UserManager {

	private $registry;
	

	public function __construct() {
		$this->registry = Yapary_SingletonRegistry::getInstance();
		return;
	
	}

	
	public function emailExist($_email) {
		
		if($_email == null || $_email == "") {
			
			return Error_ErrnoUser::$ERRNO_EMPTY;
		}
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;
		
		$count = $oSql->request('SELECT UserId FROM User WHERE Email =:email',
				array('email'	=> $_email));
		
		if($count->getCount() <= 0) {
			return Error_ErrnoUser::$ERRNO_EMAIL_NOT_EXIST;
		}
		else {
			return $count->getCount();
		}
	
	}

	public function editUser($_user, $_deliveryAdressEdited, $_billAdressEdited) {
		

		/* @var $oUser User */
		$oUser = $_user;
		if($oUser == null) {
			return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
		}

		
		/* @var $oRole Role */
		$oRole = $oUser->getRole();
		if($oRole == null) {
			return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
		}
		
			
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;
		
		
		if($_deliveryAdressEdited == true) {

			/* @var $oAddressDelivery Address */
			$oAddressDelivery = $oUser->getDeliveryAddress();
			if($oAddressDelivery == null) {
				return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
			}

			$oSql->request('UPDATE Adresse SET Ort=:city,
							Strasse=:street,
							Plz=:plz,
							LaenderCode=:code,
							TelefonNr=:tele
							WHERE AdressId=:id',
							array('city'	=> $oAddressDelivery->getCity(),
									'street' => $oAddressDelivery->getStreet(),
									'plz'	=> $oAddressDelivery->getPlz(),
									'code'	=> $oAddressDelivery->getCountryCode(),
									'tele'	=> $oAddressDelivery->getTelephone(),
									'id'	=> $oAddressDelivery->getAddressId()));
		}

		if($_billAdressEdited == true) {

			/* @var $oAddressDelivery Address */
			$oAddressBill = $oUser->getBillAddress();
			if($oAddressBill == null) {
				return Error_ErrnoUser::$ERRNO_OBJECT_NULL;
			}

			$oSql->request('UPDATE Adresse SET Ort=:city,
							Strasse=:street,
							Plz=:plz,
							LaenderCode=:code,
							TelefonNr=:tele
							WHERE AdressId=:id',
							array('city' => $oAddressBill->getCity(),
									'street' => $oAddressBill->getStreet(),
									'plz' => $oAddressBill->getPlz(),
									'code' => $oAddressBill->getCountryCode(),
									'tele' => $oAddressBill->getTelephone(),
									'id' => $oAddressBill->getAddressId()));
		}

		$oSql->request('UPDATE User SET Passwort=:pw,
						Email=:email,
						Newsletter=:news,
						Status=:status,
						Vorname=:firstname,
						Nachname=:lastname,
						besitztRolle=:role,
						hatLieferAdresse=:deliver,
						hatRechnungsAdresse=:bill
						WHERE Email=:email',
						array('pw' => $oUser->getPassword(),
								'news' => $oUser->getNewsletter(),
								'status' => $oUser->getStatus(),
								'firstname' => $oUser->getFirstname(),
								'lastname' => $oUser->getLastname(),
								'role' => $oRole->getRoleId(),
								'deliver' => $oAddressDelivery->getAddressId(),
								'bill' => $oAddressBill->getAddressId(),
								'email' => $oUser->getEmail()));

		return 1;
	}


	public function loginUserByDb($_userName, $_password) {

		if($_userName == NULL || $_password == NULL) {
			return Error_ErrnoUser::OBJECT_NULL;
		}
		
		if($_userName == "" || $_password == "") {
			return Error_ErrnoUser::$ERRNO_STRING_EMPTY;
		}
		
		if($this->isLoggedIn($_userName) > 0) {
			return Error_ErrnoUser::$ERRNO_USER_ALREADY_LOGGEDIN;
		}
		
		
		$email = $_userName;
		$password = $_password;
		
		$this->registry = Yapary_SingletonRegistry::getInstance();
		
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;

		$user = $this->checkPassGetUser($email, $password);
		
		if(!isset($user) || $user == NULL) {
			return Error_ErrnoUser::$ERRNO_USER_OR_PW_WRONG;
		}
		$userIdDb = $user->getNext()->UserId;
		$_SESSION['username'] = $email;
		$oSql->request('UPDATE User SET UserSession = "' . session_id() . '" WHERE UserId =:userId',
				array('userId'	=>	$userIdDb));
		return 1;
		
	}


	public function logoutUserbyDb($_userName) {
		if(!$this->isLoggedIn($_userName)) {
			return Error_ErrnoUser::$ERRNO_USER_ALREADY_LOGGEDIN;
		}
		$registry = Yapary_SingletonRegistry::getInstance();
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;
		$oSql->request('UPDATE user SET UserSession = NULL WHERE UserSession =:session',
				array('session' => session_id()));
		
		return 1;
	}

	
	public function isLoggedIn($_userName) {
		$registry = Yapary_SingletonRegistry::getInstance();
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $registry->oSql;		
		$result = $oSql->request('SELECT Email FROM User WHERE UserSession =:session',
				array('session'	=>	session_id()));
		if($result->getCount() == 1) {
			if($result->getNext()->Email == $_userName) {
				return true;
			}			
		}
		return Error_ErrnoUser::$ERRNO_USER_NOT_LOGGED_IN;		
	}
	
	
	public function checkPassGetUser($_user, $_password) {
		/* @var $oSql Yapary_Db_Sql_Connector */
		$oSql =  $this->registry->oSql;
	
		$user = $oSql->request('SELECT * FROM User WHERE Email=:email AND Passwort =:pass',
				array('email'	=> $_user,
						'pass'	=> $_password));
		if(!isset($user) || $user == null || $user->getCount() == 0) {
			return NULL;
		}
		else {
			return $user;
		}
	
	}
	
	
	
	public function checkLoginTime($_user) {
		
	}
	

}


?>
